package com.zlaidyrac.weride.ui;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import com.zlaidyrac.weride.main.Common;
import com.zlaidyrac.weride.main.R;
import com.zlaidyrac.weride.webclient.WebClient;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MyStats extends Activity {
	public static final String FB_IMAGE_PATH = "weride/fb_img.png";
	
	ArrayList<String> locationlist;
	private ListView mainListView;
	private ArrayAdapter<String> listAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mystats);
		TextView nameTextView = (TextView) findViewById(R.id.nameTextView);
		nameTextView.setText(Common.loggedUser.getName());
		new DownloadFbImageAsyncTask().execute();

		// Find the ListView resource.
		// mainListView = (ListView) findViewById( R.id.m );

		// Create ArrayAdapter using the planet list.
		// listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
		// locationlist);

		// Set the ArrayAdapter as the ListView's adapter.
		// mainListView.setAdapter( listAdapter );
	}

	
	public void loadFbImage() {
		
		ImageView fbImageView = (ImageView) findViewById(R.id.profPicImageView);
		fbImageView.setImageBitmap(BitmapFactory.decodeFile(
				Environment.getExternalStorageDirectory()+"/"+FB_IMAGE_PATH));
	}
	private class DownloadFbImageAsyncTask extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... params) {
			File f = new File(Environment.getExternalStorageDirectory()+"/"+FB_IMAGE_PATH);
			if ( !f.exists() ) {
				return true;
			}
			boolean decodeSuccess = WebClient.loadImageFromWebOperations(
					"http://graph.facebook.com/"
							+ Common.loggedUser.getFbUrl().trim()
							+ "/picture", Environment.getExternalStorageDirectory()+"/"+FB_IMAGE_PATH );
			
			return decodeSuccess;
		}
		protected void onPostExecute(Boolean success) {
			super.onPostExecute(success);
			if ( success ) {
				Log.i("Fb Image Load:", "Successful");
				loadFbImage();
			}
			else {
				Log.i("Fb Image Load:", "Fail");
			}
		}

	}

}
